//Firebug Please. Copyright (c) 2011, Joe Ray Gregory <j.r.gregory@gmx.de>, MIT Style License.


/*  Cookie Action Class
    # Makes cookie handling easier
    # Methods:
        - init = Loads any initial methods. In this case createCookieHash. 
        - cookieToArray = Loads the current Cookie and split it into an array
        - createCookieHash = Generates an Hashmap from the ugly cookie string  
        - getItem: Hlps you to Select one item
        - updateItem: Update a item in the cookie. It executes the updateCookie method
*/



var CookieAction = function() {
    this.init = function() {
        this.createCookieHash();
    };
    this.cookieToArray = document.cookie.split(';');
    this.cookieHashmap = new Array();
    this.createCookieHash = function() {
        for(var i =0;i<this.cookieToArray.length;i++)
        {
            var convert = this.cookieToArray[i].split('=');
            var valueOne = convert[0].replace(/^\s/, '');
            this.cookieHashmap[valueOne] = convert[1];
        }
    };
    this.getItem = function(key){
        return this.cookieHashmap[key];
    };
    this.updateItem = function(key, value) {
        this.cookieHashmap[key] = value;
        return  this.updateCookie(key, value);
    };
    this.updateCookie = function(key, value){
        return document.cookie = key+'='+value;
    };
}

var FirebugPlease = function() {
    this.currentCookie = new CookieAction();
    //options
    this.fbPOptions = {
        'src': 'https://getfirebug.com/firebug-lite.js',
        'setFbInto': document.getElementsByTagName('body')[0],
        'buttonClass': 'fbPleaseClass',
        'buttonId': 'fbPleaseId',
        'setButtonInto': document.getElementsByTagName('body')[0],
        'buttonTextOff': 'Enable Firebug',
        'buttonTextOn': 'Disable Firebug'
    };
    this.init = function() {
        //create Cookie Instance
        if(!document.cookie || !document.cookie.match(/firebug=/))
            document.cookie = "firebug=0";
        this.currentCookie.init();
        this.generateButton();
    };
};

FirebugPlease.prototype = {
    generateButton: function() {
        var _button = document.createElement('div');
        _button.setAttribute('id', this.fbPOptions.buttonId);
        _button.setAttribute('class', this.fbPOptions.buttonClass);
        _button.innerHTML = this.fbPOptions.buttonTextOff;
        this.fbPOptions.setButtonInto.appendChild(_button);
        var test = this.currentCookie;
        if(this.currentCookie.getItem('firebug')=='0')
        {
            this.setAction(_button, this.fbPOptions, this.currentCookie, false);
        } else if(this.currentCookie.getItem('firebug')=='1') {
            this.loadFirebug(this.fbPOptions, _button);
            this.setAction(_button, this.fbPOptions, this.currentCookie, true);
        }
    },
    setAction: function(button, options, cookie, active) {
        button.onclick = function() {
            if(active == false)
            {
                cookie.updateItem('firebug','1');
                var firebug=document.createElement('script');
                firebug.setAttribute('src',options.src);
                firebug.setAttribute('id','fireBugScript');
                options.setFbInto.appendChild(firebug);
                button.innerHTML = options.buttonTextOn;
            }  else if (active == true)
            {
                var script = document.getElementById('fireBugScript');
                options.setFbInto.removeChild(document.getElementById('FirebugUI'));
                options.setFbInto.removeChild(script);
                button.innerHTML = options.buttonTextOff;
                cookie.updateItem('firebug','0');
            }
            location.href = location.href;
        }
    },
    loadFirebug: function(options, button) {
        var firebug=document.createElement('script');
        firebug.setAttribute('src',options.src);
        firebug.setAttribute('id','fireBugScript');
        options.setFbInto.appendChild(firebug);
        button.innerHTML = options.buttonTextOn;
    }
    
};
/*    this.setFirebug = function() {
        
        alert(this);
        this.currentCookie.updateItem('firebug','1');
        var firebug=document.createElement('script');
        firebug.setAttribute('src','https://getfirebug.com/firebug-lite.js');
        firebug.setAttribute('id','fireBugScript');
        this.fbPOptions.setFbInto.appendChild(firebug);
        
    };
    this.removeFirebug = function() {
        var script = document.getElementById('fireBugScript');
        this.fbPOptions.setFbInto.removeChild(document.getElementById('FirebugUI'));
        this.fbPOptions.setFbInto.removeChild(script);
        this.currentCookie.updateItem('firebug','0');
    };
    */